<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;

class Play extends Command
{
    protected $client;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'play';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '游乐场';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        set_time_limit(1000);

        $base_url = 'http://www.365yg.com';
        $header_params = [
            'Host' => $base_url,
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Cache-control:' => 'max-age=0',
            'Connection' => 'keep-alive',
            'Dnt' => '1',
            'Referer' => 'https://www.toutiao.com',
            'Upgrade-Insecure-Requests' => '1',
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        ];
        //

        $jar = new \GuzzleHttp\Cookie\CookieJar();
        $this->client = new Client([
            'headers' => $header_params,
            'allow_redirects' => true,
            'base_uri' => $base_url,
            'http_errors' => false,
            //'cookies' => $jar
        ]);
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
//
//        Episode::orderBy('created_at')->chunk(500, function ($episode) {
//            foreach ($episode as $ep) {
//                $episode_raw_name = $ep->raw_name;
//                echo $episode_raw_name . "\n";
//                $episode_raw_name = filterEpisodeRawName($episode_raw_name);
//
//                $ep->name = $episode_raw_name;
//                $ep->timestamps = false;
//                $ep->save();
//                echo $episode_raw_name;
//                echo "\n--------------------------------------------------------------------------------------------------------------- \r\n";
//
//            }
//        });

        //$this->disk();
//        Article::with('images')->chunk(50, function ($ars) {
//            foreach ($ars as $ar) {
//                if ($ar->images->count() > 0 && empty($ar->coverage_uri)) {
//                    $ar->coverage_uri = $ar->images->first()->key;
//                    $ar->save();
//                    var_dump($ar->coverage_uri);
//                }
//            }
//
//        });


//        Subtitle::chunk(200, function ($subsss) {
//            foreach ($subsss as $sub) {
//                $sub->url = $sub->raw_url;
//                $sub->name = $sub->raw_name;
//                var_dump($sub->url);
//                $sub->save();
//            }
//
//        });


//        $urls = [
//            'https://www.toutiao.com/i6445609592238899726/',
//        ];
//
//        for ($i = 0; $i < 10000; $i++) {
//
//            foreach ($urls as $href) {
//
//                $response = $this->client->request('GET', $href);
//                if ($code = $response->getStatusCode() == 200) {
//                    echo $response->getBody();
//                    echo $code . "\n";
//                } else {
//                    $msg = $href . '网页加载失败';
//                    \Log::info($msg);
//                    echo $msg . "\n";
//                }
//
//            }
//            usleep(rand(300, 600));
//        }

        $this->videoParse();
    }


    public function videoParse($url = 'https://v.qq.com/x/cover/v6lhv3xr1k5cy5g/p05366fqpxc.html')
    {
        // echo $url;
        $cmd = "export LANG=en_US.UTF-8; you-get --json $url";

        exec($cmd, $ret);
        dd(implode('', $ret));
    }

    public function deleteTrash()
    {
        $articles = Tag::first(1)->articles()->delete();
    }
}